package com.atung.merchant.controller;

import com.atung.merchant.result.ApiResult;
import com.atung.merchant.service.MerchantService;
import com.atung.merchant.vo.IdRequest;
import com.atung.merchant.vo.ResponseMerchantInfo;
import com.atung.merchant.vo.save.SaveMerchantRequest;
import com.atung.merchant.vo.save.SaveMerchantUserRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/merchant")
@Api(tags = "商家管理")
public class MerchantController {

    @Resource
    private MerchantService merchantService;

    @PostMapping("/update")
    @ApiOperation("修改商家")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult saveMerchant(@RequestBody SaveMerchantRequest saveMerchantRequest) {
        return merchantService.saveMerchantService(saveMerchantRequest);
    }

    @PostMapping("/query/details")
    @ApiOperation("获取商家信息")
    public ApiResult<ResponseMerchantInfo> queryMerchantInfo() {
        try {
            return ApiResult.ok(merchantService.queryMerchantInfoService());
        } catch (Exception e) {
            return ApiResult.fail();
        }
    }

    @PostMapping("/save/user")
    @ApiOperation("新增用户")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult saveMerchantUser(@RequestBody @Valid
                                          SaveMerchantUserRequest saveMerchantUserRequest) {
        try {
            return merchantService.saveMerchantUserService(saveMerchantUserRequest);
        } catch (Exception e) {
            return ApiResult.fail();
        }
    }

    @PostMapping("/delete/user")
    @ApiOperation("删除用户")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult deleteMerchantUser(@RequestBody @Valid IdRequest idRequest) {
        try {
            return merchantService.deleteMerchantUserService(idRequest);
        } catch (Exception e) {
            return ApiResult.fail();
        }
    }
    @PostMapping("/query/user")
    @ApiOperation("获取用户")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult queryMerchantUser() {
        try {
            return ApiResult.ok(merchantService.queryMerchantUserService());
        } catch (Exception e) {
            return ApiResult.fail();
        }
    }


}
